from pynput import keyboard  # 导入键盘类
import pygame
import threading  # 导入线程包


class Player():
    def __init__(self):
        self.standmode = r"八神stand"

        self.sd1 = pygame.image.load(self.standmode + r"\data\001.png").convert_alpha()
        self.sd2 = pygame.image.load(self.standmode + r"\data\002.png").convert_alpha()
        self.sd3 = pygame.image.load(self.standmode + r"\data\003.png").convert_alpha()
        self.sd4 = pygame.image.load(self.standmode + r"\data\004.png").convert_alpha()
        self.sd5 = pygame.image.load(self.standmode + r"\data\005.png").convert_alpha()
        self.sd6 = pygame.image.load(self.standmode + r"\data\006.png").convert_alpha()
        self.sd7 = pygame.image.load(self.standmode + r"\data\007.png").convert_alpha()
        self.sd8 = pygame.image.load(self.standmode + r"\data\008.png").convert_alpha()
        self.stand = [self.sd1, self.sd2, self.sd3, self.sd4, self.sd5, self.sd6, self.sd7, self.sd8]
        self.run1 = pygame.image.load(r"八神run\data\001.png").convert_alpha()
        self.run2 = pygame.image.load(r"八神run\data\002.png").convert_alpha()
        self.run3 = pygame.image.load(r"八神run\data\003.png").convert_alpha()
        self.run4 = pygame.image.load(r"八神run\data\004.png").convert_alpha()
        self.run5 = pygame.image.load(r"八神run\data\005.png").convert_alpha()
        self.run6 = pygame.image.load(r"八神run\data\006.png").convert_alpha()
        self.run7 = pygame.image.load(r"八神run\data\007.png").convert_alpha()
        self.run = [self.run1, self.run2, self.run3, self.run4, self.run5, self.run6, self.run7]
        self.state = "sd"
        self.runrecord = 0
        self.pos = [60, 400]  # 初始化人物位置
        self.speed = 5  # 初始化人物移动速度
        self.midspeed = 15
        self.sdrecord = 0


    def movement(self):
        if self.state == "sd":
            if self.sdrecord < len(self.stand)-1:
                self.sdrecord += 1
                self.pic = self.stand[self.sdrecord]  # 初始化人物图像
            else:
                self.sdrecord = 0
                self.pic = self.stand[self.sdrecord]  # 初始化人物图像
        elif self.state == "run":
            if self.runrecord < len(self.run)-1:
                self.runrecord += 1
                self.pic = self.stand[self.runrecord]  # 初始化人物图像
            else:
                self.runrecord = 0
                self.pic = self.stand[self.runrecord]  # 初始化人物图像

    # 定义按键监听函数
    def on_press(self, key):
        print(self.state)
        '按下按键时执行。'
        if key == keyboard.Key.up:
            self.pos[1] -= self.speed
        elif key == keyboard.Key.down:
            self.pos[1] += self.speed
        elif key == keyboard.Key.right:
            self.state = "run"
            self.pos[0] += self.midspeed
        elif key == keyboard.Key.left:
            self.state = "run"
            self.pos[0] -= self.midspeed
        else:
            pass

    # 通过属性判断按键类型。

    def on_release(self, key):
        '松开按键时执行。'
        self.state = "sd"
        if key == keyboard.Key.esc:
            # Stop listener
            return False
        else:
            pass

    def lis(self):
        with keyboard.Listener(
                on_press=self.on_press,
                on_release=self.on_release) as listener:
            listener.join()


def main():
    pygame.init()
    fps = pygame.time.Clock()
    screen = pygame.display.set_mode((1240, 825))
    pygame.display.set_caption("RPG游戏")
    backgroud = pygame.image.load(r"口袋英雄类 横版场景 手绘背景地图-01_爱给网_aigei_com.jpg").convert_alpha()
    link = Player()  # 实例化主角
    t = threading.Thread(target=link.lis, args=())  # 定义线程
    t.start()  # 运行线程
    while True:
        screen.blit(backgroud, (0, 0))
        if link.state == "sd":
            screen.blit(link.stand[link.sdrecord], (link.pos[0], link.pos[1]))
        if link.state == "run":
            screen.blit(link.run[link.runrecord], (link.pos[0], link.pos[1]))
        pygame.display.flip()
        fps.tick(25)
        link.movement()


main()
